import pandas as pd
import ast
import re


def get_stardquestion_df():
    file_name = r"D:\工作相关内容\公司项目\智能机器人\IM已圈定场景（只交互一次）.xlsx"
    df = pd.read_excel(file_name)
    df.dropna(subset=["服务标准"],inplace=True)
    df = df[["一级业务标签", "二级业务标签", "三级业务标签", "服务标准", "标注的意图名称"]]

    def clear_question(question):
        a = 1
        return re.sub('\（.*\）',"",question)
    df['服务标准'] = df['服务标准'].apply(clear_question)
    print(df.head())

    return df
question_df = get_stardquestion_df()

def apply_make_dialogue(dialogues):
    json_obj = ast.literal_eval(dialogues)
    output = ""
    for item in json_obj:
        if item["role"] == "客户":
            item["role"] = "用户"
        contents = item["content"].split("\n")
        new_content = ""
        for index,c in enumerate(contents):
            if index == 0:
                new_content += c +"\n"
            else:
                break
                #new_content += "           " + c + "\n"
        new_content = new_content[:-1]
        if item["msg_type"] == 'card':
            msg_type = item["card_type"]
        else:
            msg_type = item["msg_type"]
        output += item["role"]+"("+msg_type+")"+": " + new_content+ "\n"
    return output


def apply_hit_question(detail):
    questions = question_df["服务标准"].tolist()
    for q in questions:
        if q in detail:
            return q
    return ""



def make_doccano_file(file_path,sample=True):
    df = pd.read_csv(file_path,sep='\001')



    df['text'] = df['dialogues'].apply(apply_make_dialogue)
    df["IM_question"] = df['conversation_record_detail'].apply(apply_hit_question)


    df = df[["global_conversation_id","major_biz_name","minor_biz_name","minimal_biz_name","IM_question",'text',"ai_turns"]]
    df.rename(columns={"major_biz_name":"一级业务类型","minor_biz_name":"二级业务类型","minimal_biz_name":"三级业务类型",},inplace=True)



    df_list = []

    label_intention_names = question_df['标注的意图名称'].unique()
    for intention_name in label_intention_names:
        if intention_name not in ["技术服务费未退",
"信息费退款规则",
"取消订单",
"不满意投诉处理结果",
"放空费纠纷",
"用户未收到退款",
"放空费纠纷",
"撤销投诉",
"信息费纠纷",
"如何收取运费",
"拉跑货纠纷",
"迟到爽约纠纷"]:
            continue


        df_temp_list = []
        IM_questions = question_df[question_df["标注的意图名称"] == intention_name]["服务标准"].tolist()
        for question in IM_questions:
            df_temp_list.append(df[df["IM_question"] == question])
        df_temp_merge = pd.concat(df_temp_list)
        df_temp_merge["IM_question"] = intention_name

        print(intention_name + ":"+ str(df_temp_merge.shape[0]))
        if df_temp_merge.shape[0] <=1000:
            third_tags_names = question_df[question_df["标注的意图名称"] ==  intention_name]["三级业务标签"].unique()
            df_tags = []
            for question in third_tags_names:
                if question == "撤销投诉":
                    df_tags.append(df[df["二级业务类型"] == "客诉处理中"])
                elif question == "收取运费":
                    df_tags.append(df[df["二级业务类型"] == "运费"])
                elif question == "咨询如果司机拉跑货该如何处理":
                    df_tags.append(df[df["二级业务类型"] == "拉跑货"])
                else:
                    df_tags.append(df[df["三级业务类型"] == question])
            df_tags = pd.concat(df_tags)
            df_temp_merge["IM_question"] = intention_name

            add_num = 1000 - df_temp_merge.shape[0]
            df_list.append(pd.concat([df_temp_merge,df_tags.sample(n=add_num, random_state=1)]))
        else:
            df_list.append(df_temp_merge.sample(n=1000, random_state=1))

    df_output = pd.concat(df_list)




    df_output.to_json("doccao.jsonl",orient="records",lines=True,force_ascii=False)





if __name__ == '__main__':
    file_path = r'D:\工作相关内容\公司项目\智能机器人\20230216.csv'

    #get_stardquestion_df()
    make_doccano_file(file_path,sample=True)